package com.qf.service;

import com.qf.config.IdWorker;
import com.qf.constant.RabbitMQConstants;
import com.qf.constant.ReportStateConstants;
import com.qf.constant.SourceType;
import com.qf.form.SmsInterfaceForm;
import com.qf.model.StandardSubmit;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/*** @author xl
 * @version V1.0
 * @Project JavaGP12
 * @Package com.qf.service
 * @Description: ${TODO}
 *@Date 2022/8/24 17:37
 */
@Service
public class InterfaceServiceImpl implements InterfaceService{

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    /**
     *      封装对象  发消息到MQ中
     * @param smsInterfaceForm
     */
    @Override
    public void submitInterfaceForm(SmsInterfaceForm smsInterfaceForm) {
        //发送消息
        System.out.println("===发送消息成功===");
        //手机号
        String mobile = smsInterfaceForm.getMobile();
        if (!StringUtils.isEmpty(mobile)){
            String[] phones = mobile.split(",");
            for (String phone : phones) {
                StandardSubmit standardSubmit=new StandardSubmit();
                //当前唯一标识
                standardSubmit.setSequenceId(String.valueOf(idWorker.nextId()));
                //客户ID
                standardSubmit.setClientID(Integer.parseInt(smsInterfaceForm.getClientID()));
                //客户侧唯一序列号
                standardSubmit.setSrcSequenceId(Long.parseLong(smsInterfaceForm.getSrcID()));
                //手机号
                standardSubmit.setDestMobile(phone);
                //短信内容
                standardSubmit.setMessageContent(smsInterfaceForm.getContent());
                //手机接收的状态
                standardSubmit.setReportState(ReportStateConstants.WAIT);
                //发送时间
                standardSubmit.setSendTime(new Date());
                //发送方式
                standardSubmit.setSource(SourceType.HTTP);
                //发送消息
                rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PRE_SEND+"_exchange",  //交换机
                        //RabbitMQConstants.BINDING,   //RoutingKey 路由
                        "",
                        standardSubmit);
            }
        }

    }



}
